package cn.panda.edu.Login;

import cn.panda.edu.model.Student;
import cn.panda.edu.model.Teacher;
import cn.panda.edu.util.Constant;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 登陆拦截
 * @create 2019-03-06 15:13
 */
@Controller
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {
    Logger log = Logger.getLogger(LoginInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception{
        String basePath = request.getContextPath();
        String path = request.getRequestURI();
//      log.info("basePath:"+basePath);
//      log.info("path:"+path);

        if(!doLoginInterceptor(path, basePath) ){//是否进行登陆拦截
            return true;
        }
        //如果登录了，会把用户信息存进session
        HttpSession session = request.getSession();
        Student student = (Student) session.getAttribute(Constant.LOGIN_USER);
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        if(!(student!=null|| teacher!=null)){
            /*log.info("尚未登录，跳转到登录界面");
            response.sendRedirect(request.getContextPath()+"signin");*/

            String requestType = request.getHeader("X-Requested-With");
//          System.out.println(requestType);
            if(requestType!=null && requestType.equals("XMLHttpRequest")){
                response.setHeader("sessionstatus","timeout");
//              response.setHeader("basePath",request.getContextPath());
                response.getWriter().print("LoginTimeout");
                return false;
            } else {
                log.info("尚未登录，跳转到登录界面");
                response.sendRedirect(request.getContextPath()+"/");
            }
            return false;
        }
      log.info("用户已登录");
        return true;
    }

    /**
     * 是否进行登陆过滤
     * @param path
     * @param basePath
     * @return
     */
    private boolean doLoginInterceptor(String path,String basePath){
        path = path.substring(basePath.length());
        Set<String> notLoginPaths = new HashSet<>();
        //设置不进行登录拦截的路径：登录注册和验证码
        //notLoginPaths.add("/");
        notLoginPaths.add("/");
        notLoginPaths.add("/api/teacher/login");
        notLoginPaths.add("/api/student/login");
//        notLoginPaths.add("/signin");
//        notLoginPaths.add("/login");
//        notLoginPaths.add("/register");
//        notLoginPaths.add("/kaptcha.jpg");
//        notLoginPaths.add("/kaptcha");
        //notLoginPaths.add("/sys/logout");
        //notLoginPaths.add("/loginTimeout");

        if(notLoginPaths.contains(path)) return false;
        return true;
    }

}
